<?php
class CourseServer {
	public function pagingBySearch($where="",$order='',$numberEachPage = 10){
		$count = HttpDaoManager::getCourseDao()->searchNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getCourseDao()->searchList($where,$order, $limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	public function deleteBatchByid($id){
		if (is_array($id)){
			$id = implode(',',$id);
		}
		HttpDaoManager::getCourseDao()->deleteBatchByid($id);
	}
	public function add($data){
		$courseDao = HttpDaoManager::getCourseDao();
		$tmp =$courseDao->getSerialNumber($data['serial_number']);
		$data['homemade_time'] = strtotime($data['homemade_time']);
		$data['book_time'] = strtotime($data['book_time']);
		$data['for_purpose_study'] = '';
		if(!empty($_POST['for_purpose_study'])){
			$data['for_purpose_study'] = implode(',',$_POST['for_purpose_study']);
		}
		$data['for_purpose_people'] = '';
		if(!empty($_POST['for_purpose_people'])){
			$data['for_purpose_people'] = implode(',',$_POST['for_purpose_people']);
		}
		$data['course_recommend'] = 0;
		if(isset($_POST['course_recommend'])&&$_POST['course_recommend']>0){
			$data['course_recommend'] = 1;
		}
		
		if (!empty($tmp)){
			return  ERROR_COURSE_ONE;
		}
		else{
			if ($data['source']==COURSE_BOOK){
				$data['homemade_time'] = 0;
				$data['homemade_producer'] = '';
			}
			else{
				$data['book_barcode'] = '';
				$data['book_press'] = '';
				$data['book_version'] = '';
				$data['book_time'] = 0;
			}
			$courseDao->add($data);
		}
		return true;
	}
	public function update($data,$id){
		$data['homemade_time'] = strtotime($data['homemade_time']);
		$data['book_time'] = strtotime($data['book_time']);
			$data['for_purpose_study'] = '';
		if(!empty($_POST['for_purpose_study'])){
			$data['for_purpose_study'] = implode(',',$_POST['for_purpose_study']);
		}
		$data['for_purpose_people'] = '';
		if(!empty($_POST['for_purpose_people'])){
			$data['for_purpose_people'] = implode(',',$_POST['for_purpose_people']);
		}
		
		$data['course_recommend'] = 0;
		if(isset($_POST['course_recommend'])&&$_POST['course_recommend']>0){
			$data['course_recommend'] = 1;
		}		
		if ($data['source']==COURSE_BOOK){
			$data['homemade_time'] = 0;
			$data['homemade_producer'] = '';
		}
		else{
			$data['book_barcode'] = '';
			$data['book_press'] = '';
			$data['book_version'] = '';
			$data['book_time'] = 0;
		}
		HttpDaoManager::getCourseDao()->update($data, $id);
	}
	public function userGetCourseType(){
		$courseType = Common::_t('course_type');
		$course = array();
		
		$type = array();
		$userCourse = array();
		$userChapter = array();
		$userChapters = array();
		foreach ($courseType as $k=>$v){
			$course_cnt =$this->userGetListByTypeCnt($k);
			$k = intval($k);
			if ($k==COURSE_TYPE_SPECIAL){
				$type[$k]=array('deep'=>"1");
			}
			else{
				if ($course_cnt==1){
					$type[$k]=array('deep'=>"2");
				}else{
					$type[$k]=array('deep'=>"3");
				}
			}
		}
		return array('type'=>$type);
	}
	
	public function userGetCourse($course_type_id){
		$userCourse = array();
		$course =$this->userGetCoursePairList($course_type_id) ;
		$tmp1 = array();
		foreach ($course as $v){
			$tmp1[$v['id']] = array('coursename'=>$v['theme']);
		}
		$userCourse = $tmp1;		
		return array('course'=>$userCourse);
	}
	
	public function filterSummerActiver(&$data){
		$userInfo = HttpDaoManager::getUserDao()->get($_SESSION['user']['id']);
		$userKindActiveFlg = HttpServerManager::getUserServer()->validateUserActiveKind($userInfo);
		if($userKindActiveFlg==true){
			$targetCourseList = Common::_t('target_course_list');
			$initCourseArray = $targetCourseList[$userInfo['user_kind']];
			$init_selected_course_list = $initCourseArray['course_list'];
			if(!empty($init_selected_course_list)){
				foreach($data['course'] as $k=>$v){
					if(!in_array($k,$init_selected_course_list)){
						unset($data['course'][$k]);
					}
				}
			}
		}
	}
	
	
	public function userGetChapter($course_id){
		$userChapter = array();
		$chapterServer=HttpServerManager::getChapterServer();
		$chapter =$chapterServer->userGetPairListByCourseId($course_id);
		$tmp = array();
		foreach ($chapter as $v){
			$tmp[$v['id']] = array('chaptername'=>$v['name'].'-'.$v['english_name']);;
		}
		$userChapter = $tmp;		
		return array('chapter'=>$userChapter);
	}
	
	public function userGetCourse2(){
		$courseType = Common::_t('course_type');
		$course = array();
		
		$type = array();
		$userCourse = array();
		$userChapter = array();
		$userChapters = array();
		$chapterServer=HttpServerManager::getChapterServer();
		foreach ($courseType as $k=>$v){
			$course[$k] =$this->userGetListByType($k) ;
			$k = intval($k);
			if ($k==COURSE_TYPE_SPECIAL){
				$type[$k]=array('deep'=>"1");
			}
			else{
				if (count($course[$k])==1){
					$type[$k]=array('deep'=>"2");
					$chapter = $chapterServer->userGetListByCourseId($course[$k][0]['id']);
					$tmp = array();
					foreach ($chapter as $v1){
						$tmp[$v1['id']] = array('coursename'=>$v1['name'],'is_course'=>'');
					}
					$userCourse[$k] = $tmp;
				}else{
					$type[$k]=array('deep'=>"3");
					$tmp1 = array();
					foreach ($course[$k] as $v2){
						$tmp1[$v2['id']] = array('coursename'=>$v2['theme'],'is_course'=>'1');
						$userChapter =$chapterServer->userGetListByCourseId($v2['id']);
						$tmp2 = array();
						foreach ($userChapter as $v3){
							$tmp2[$v3['id']] = $v3['name'];
						}
						$userChapters[$v2['id']] = $tmp2;
					}
					$userCourse[$k] = $tmp1;
				}
			}
		}
		return array('type'=>$type,'course'=>$userCourse,'chapter'=>$userChapters);
	}
	
	
	public function downloadMaterial($chapter,$course){
		$path= "";
		if (!empty($chapter)){
			$chapterData = HttpDaoManager::getChapterDao()->get($chapter);
			$path = $chapterData['document'];
		}
		else{
			$courseData = HttpDaoManager::getCourseDao()->get($course);
			$path = $courseData['teaching_material'];
		}
		if (!empty($path)){
			return  $path;
		}
		else{
			return USER_ERROR_TWO;
		}
	} 
	public function userGetCoursePairList($type){
		$data  = array(
			'type' =>$type,
			'status' =>1
		);
		return HttpDaoManager::getCourseDao()->searchPairList($data);
	}
	
	public function userGetListByType($type){
		$data  = array(
			'type' =>$type,
			'status' =>1
		);
		return HttpDaoManager::getCourseDao()->searchList($data);
	}
		
	public function userGetListByTypeCnt($type){
		$data  = array(
			'type' =>$type,
			'status' =>1
		);
		return HttpDaoManager::getCourseDao()->searchNumber($data);
	}
	public function userGet($id){
		$data  = array(
			'id' =>$id,
			'status' =>1
		);
		return HttpDaoManager::getCourseDao()->searchList($data);
	}
	public function subBookGetCourse($type,$chapter){
		if ($type==COURSE_TYPE_SPECIAL){
			$coursedata = HttpServerManager::getCourseServer()->userGetListByType(COURSE_TYPE_SPECIAL);
		}
		else{
			if ($chapter==-1){
				return USER_ERROR_FOUR;
			}
			else{
				$chapterData = HttpServerManager::getChapterServer()->userGet($chapter);
				if (empty($chapterData)){
					return USER_ERROR_THREE;
				}else{
					$coursedata = HttpServerManager::getCourseServer()->userGet($chapterData[0]['course_id']);
				}
			}
		}
		if (empty($coursedata)){
			return USER_ERROR_THREE;
		}
		$coursedata = $coursedata[0];
		return $coursedata;
	}
}
?>